package com.xx.pmis.web.actions;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.HotelExpense;
import com.xx.pmis.model.TourismPlan;
import com.xx.pmis.services.HotelExpenseService;
import com.xx.pmis.services.TourismPlanService;
import com.xx.pmis.web.forms.HotelExpenseForm;
import com.xx.pmis.web.util.AccountUtil;
import com.xx.pmis.web.util.CurrentDate;
import com.xx.pmis.web.util.Rand;

/**
 * @class 住宿费Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class HotelExpenseAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(HotelExpenseAction.class);

	HotelExpenseService hotelExpenseService = getPMISServices().getHotelExpenseService();
	TourismPlanService tourismPlanService = getPMISServices().getTourismPlanService();

	AccountUtil accountUtil = new AccountUtil();

	// 查询所有住宿费
	public ActionForward listHotelExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<HotelExpense> hotelExpenseList = null;
		try {
			hotelExpenseList = hotelExpenseService.getAllHotelExpenseByCondition(" order by targetDate desc");
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费】查询【住宿费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("hotelExpenseList", hotelExpenseList);
		request.setAttribute("position", "旅游管理/住宿费");
		return mapping.findForward("listHotelExpense");
	}

	// 新增
	public ActionForward addHotelExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<TourismPlan> tourismPlanList = null;
		try {
			tourismPlanList = tourismPlanService
					.getAllTourismPlanByCondition(" where tpState = '1' order by beginDate desc");
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/新增】查询未完成【旅游计划】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		if (tourismPlanList.size() == 0) {
			String errorPage = "【旅游计划/住宿费/新增】没有未完成旅游计划，无法添加交通费用！";
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("date", CurrentDate.currentDate());
		request.setAttribute("tourismPlanList", tourismPlanList);
		request.setAttribute("position", "住宿费/新增");
		return mapping.findForward("addHotelExpense");
	}

	// 新增，处理方法
	public ActionForward addHotelExpense_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		HotelExpenseForm f = (HotelExpenseForm) form;

		String targetDate = f.getTargetDate(); // 预订日期，预订日期在结算时作为流水账记账日期
		String heName = f.getHeName(); // 酒店名称
		String heCity = f.getHeCity(); // 所在城市
		String arriveDate = f.getArriveDate(); // 入住日期
		String leaveDate = f.getLeaveDate(); // 离开日期
		String dayNumber = f.getDayNumber(); // 天数
		String heExpenses = f.getHeExpenses(); // 价格，一天的价格
		String heDesc = f.getHeDesc(); // 描述
		String tpID = f.getTpID(); // 旅游计划ID

		HotelExpense hotelExpense = new HotelExpense();
		hotelExpense.setHeID(CurrentDate.currentYear() + Rand.getRand());
		hotelExpense.setTargetDate(targetDate);
		hotelExpense.setHeName(heName);
		hotelExpense.setHeCity(heCity);
		hotelExpense.setArriveDate(arriveDate);
		hotelExpense.setLeaveDate(leaveDate);
		hotelExpense.setDayNumber(dayNumber);
		hotelExpense.setHeExpenses(accountUtil.if1ZeroTo2Zero(heExpenses));
		hotelExpense.setHeDesc(heDesc);
		hotelExpense.setHePay("1");

		// 旅游计划
		hotelExpense.setTpID(tpID);
		try {
			hotelExpenseService.saveOrUpdateHotelExpense(hotelExpense);
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/新增】保存或更新【住宿费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");

		}

		listHotelExpenseByPage(mapping, form, request, response);
		return mapping.findForward("listHotelExpense");
	}

	// 编辑
	public ActionForward editHotelExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String heID = request.getParameter("heID");
		String pageNum = request.getParameter("pageNum");

		// 住宿费
		HotelExpense hotelExpense = null;
		try {
			hotelExpense = hotelExpenseService.getHotelExpenseByHotelExpenseID(heID);
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/编辑】根据住宿费ID查询该【住宿费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		TourismPlan tourismPlan = null;
		try {
			tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(hotelExpense.getTpID());
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/编辑】根据住宿费ID查询该【旅游计划】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		List<TourismPlan> tourismPlanList = null;
		try {
			tourismPlanList = tourismPlanService
					.getAllTourismPlanByCondition(" where tpState = '1' order by beginDate desc");
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/编辑】查询未完成【旅游计划】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("pageNum", pageNum);
		request.setAttribute("tpName", tourismPlan.getTpName());
		request.setAttribute("tourismPlanList", tourismPlanList);
		request.setAttribute("hotelExpense", hotelExpense);
		request.setAttribute("position", "住宿费/编辑");
		return mapping.findForward("editHotelExpense");
	}

	// 编辑，处理方法
	public ActionForward editHotelExpense_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");

		HotelExpenseForm f = (HotelExpenseForm) form;
		String heID = f.getHeID(); // 费用ID
		String targetDate = f.getTargetDate(); // 预订日期，预订日期在结算时作为流水账记账日期
		String heName = f.getHeName(); // 酒店名称
		String heCity = f.getHeCity(); // 所在城市
		String arriveDate = f.getArriveDate(); // 入住日期
		String leaveDate = f.getLeaveDate(); // 离开日期
		String dayNumber = f.getDayNumber(); // 天数
		String heExpenses = f.getHeExpenses(); // 价格，一天的价格
		String heDesc = f.getHeDesc(); // 描述

		HotelExpense hotelExpense = null;
		try {
			hotelExpense = hotelExpenseService.getHotelExpenseByHotelExpenseID(heID);
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/编辑】根据【住宿费ID】查询该【住宿费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		hotelExpense.setTargetDate(targetDate);
		hotelExpense.setHeName(heName);
		hotelExpense.setHeCity(heCity);
		hotelExpense.setArriveDate(arriveDate);
		hotelExpense.setLeaveDate(leaveDate);
		hotelExpense.setDayNumber(dayNumber);
		hotelExpense.setHeExpenses(accountUtil.if1ZeroTo2Zero(heExpenses));
		hotelExpense.setHeDesc(heDesc);
		hotelExpense.setHePay("1");
		try {
			hotelExpenseService.saveOrUpdateHotelExpense(hotelExpense);
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/编辑】保存或更新【住宿费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("pageNum", pageNum);
		listHotelExpenseByPage(mapping, form, request, response);
		return mapping.findForward("listHotelExpense");
	}

	// 删除
	public ActionForward delHotelExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");

		// 住宿费
		HotelExpense hotelExpense = null;
		HotelExpenseForm f = (HotelExpenseForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				hotelExpense = hotelExpenseService.getHotelExpenseByHotelExpenseID(ids[i]);
			} catch (Exception e) {
				String errorPage = "【旅游计划/住宿费/删除】根据【住宿费ID】查询该【住宿费】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			try {
				hotelExpenseService.deleteHotelExpense(hotelExpense);
			} catch (Exception e) {
				String errorPage = "【旅游计划/住宿费/删除】删除【住宿费】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
		}

		request.setAttribute("pageNum", pageNum);
		listHotelExpenseByPage(mapping, form, request, response);
		return mapping.findForward("listHotelExpense");
	}

	// 结算
	public ActionForward payHotelExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String heID = request.getParameter("heID");
		String pageNum = request.getParameter("pageNum");

		// 住宿费
		HotelExpense hotelExpense = null;
		try {
			hotelExpense = hotelExpenseService.getHotelExpenseByHotelExpenseID(heID);
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/结算】根据住宿费ID查询该【住宿费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		TourismPlan tourismPlan = null;
		try {
			tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(hotelExpense.getTpID());
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/结算】根据旅游计划ID查询该【旅游计划】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		// 生成记账凭证
		// 科目代码
		String subjectCode = "";
		if (tourismPlan.getTpType().equals("0")) {
			// 旅游 0
			subjectCode = "0030005";
		} else if (tourismPlan.getTpType().equals("1")) {
			// 出差 1
			subjectCode = "0030012";
		} else {
			// 探亲 2
			subjectCode = "0030018";
		}
		accountUtil.payExpenseToAccount(hotelExpense.getTargetDate(), subjectCode, hotelExpense.getDayNumber(),
				hotelExpense.getHeExpenses(), tourismPlan.getTpName(), "/" + hotelExpense.getHeName());
		// 更新结算状态，结算完成为"0"
		hotelExpense.setHePay("0");
		try {
			hotelExpenseService.saveOrUpdateHotelExpense(hotelExpense);
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/结算】保存或更新【住宿费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("pageNum", pageNum);
		listHotelExpenseByPage(mapping, form, request, response);
		return mapping.findForward("listHotelExpense");
	}

	// 分页显示
	public ActionForward listHotelExpenseByPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int PAGESIZE = 20;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询总用户记录数
		int count = 0;
		try {
			count = hotelExpenseService.getCountOfHotelExpenseByCondition("");
		} catch (Exception e) {
			String errorPage = "【旅游管理/住宿费】查询【住宿费】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}
		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		log.info("page:" + pageNum);
		request.setAttribute("pageNum", pageNum);
		List<HotelExpense> hotelExpenseList = null;
		List<HotelExpense> hotelExpenseListTemp = new ArrayList<HotelExpense>();
		try {
			hotelExpenseList = hotelExpenseService.getAllHotelExpenseByConditionByPage(" order by targetDate desc",
					Integer.parseInt(pageNum), PAGESIZE);
		} catch (Exception e) {
			String errorPage = "【旅游管理/住宿费/分页显示】查询【住宿费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		for (HotelExpense hotelExpense : hotelExpenseList) {
			TourismPlan tourismPlan = null;
			try {
				tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(hotelExpense.getTpID());
			} catch (Exception e) {
				String errorPage = "【旅游计划/住宿费/分页显示】根据旅游计划ID查询该【旅游计划】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			hotelExpense.setTpName(tourismPlan.getTpName());
			hotelExpenseListTemp.add(hotelExpense);

		}
		request.setAttribute("hotelExpenseList", hotelExpenseListTemp);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}
		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("action", "hotelExpense.do");
		request.setAttribute("position", "旅游管理/住宿费");
		return mapping.findForward("listHotelExpense");
	}

	// 明细
	public ActionForward detailHotelExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String heID = request.getParameter("heID");

		// 住宿费
		HotelExpense hotelExpense = null;
		try {
			hotelExpense = hotelExpenseService.getHotelExpenseByHotelExpenseID(heID);
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/明细】根据【住宿费ID】查询该【住宿费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("hotelExpense", hotelExpense);
		request.setAttribute("position", "住宿费/明细");
		return mapping.findForward("detailHotelExpense");
	}

}